package com.wlyuan.open.scanner.domain.carrier;

import com.wlyuan.open.constants.CarrierTypeEnum;
import com.wlyuan.open.scanner.constant.Constants;
import com.wlyuan.open.scanner.dao.entity.SupplierDO;
import com.wlyuan.open.scanner.dao.entity.SymbolDO;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.enums.SourceInvalidEnum;
import com.wlyuan.open.scanner.utils.enums.SupplierInvalidEnum;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Optional;

/**
 * @author lijing
 * @description
 */
public class CarrierConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        /**
         * 转换
         *
         * @param source
         * @return
         */
        @Mappings({
                @Mapping(target = "contactMobile", source = "symbolTel"),
                @Mapping(target = "contactName", source = "symbolContact"),
                @Mapping(target = "name", source = "symbolName"),
                @Mapping(target = "createTime", source = "symbolInserttime"),
                @Mapping(target = "updateTime", source = "symbolUpdatetime"),
                @Mapping(target = "hots", source = "symbolHeat"),
                @Mapping(target = "comments", source = "symbolComments"),
                @Mapping(target = "code", source = "symbolCode"),
                @Mapping(target = "status", source = "symbolInvalid"),
                @Mapping(target = "id", source = "symbolId"),
                @Mapping(target = "createBy", source = "symbolCreator"),
                @Mapping(target = "updateBy", source = "symbolUpdater"),
                @Mapping(target = "tenantId", source = "symbolCompanyid"),
        })
        @BaseMapping
        Carrier convertSymbol(SymbolDO source);

        @AfterMapping
        default void after(@MappingTarget Carrier target, SymbolDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
            target.setSourceKey(String.join(Constants.STRING_JOIN, String.valueOf(source.getSymbolId()), String.valueOf(CarrierTypeEnum.OFFLINE.getValue())));
            target.setStatus(SourceInvalidEnum.fromValue(source.getSymbolInvalid()).getNewValue());
        }


        /**
         * 转换
         *
         * @param source
         * @return
         */
        List<Carrier> convertSymbol(List<SymbolDO> source);

        /**
         * 转换
         *
         * @param source
         * @return
         */
        @Mappings({
                @Mapping(target = "welcome", source = "supplierWelcome"),
                @Mapping(target = "createTime", source = "supplierInserttime"),
                @Mapping(target = "updateTime", source = "supplierUpdatetime"),
                @Mapping(target = "hots", source = "supplierHeat"),
                @Mapping(target = "id", source = "supplierId"),
                @Mapping(target = "abbr", source = "supplierShortname"),
                @Mapping(target = "comments", source = "supplierComments"),
                @Mapping(target = "code", source = "supplierClientcode"),
                @Mapping(target = "status", source = "supplierInvalid"),
                @Mapping(target = "createBy", source = "supplierCreator"),
                @Mapping(target = "updateBy", source = "supplierUpdater"),
                @Mapping(target = "autoBalance", source = "supplierAutobalance"),
                @Mapping(target = "downstream", source = "supplierCompanyid"),

                @Mapping(target = "tenantId", source = "supplierOwnercompanyid"),
        })
        @BaseMapping
        Carrier convertSupplier(SupplierDO source);

        @AfterMapping
        default void after(@MappingTarget Carrier target, SupplierDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
            target.setSourceKey(String.join(Constants.STRING_JOIN, String.valueOf(source.getSupplierId()), String.valueOf(source.getType())));
            if (source.getCompanyStatus() == null || !Constants.COMPANY_STATUS.equals(source.getCompanyStatus())) {
                target.setStatus(SupplierInvalidEnum.INVALID_NO.getValue());
            } else {
                Optional.ofNullable(MappingInvalid.createSupplierStatus(source.getSupplierStatus(), source.getSupplierInvalid()))
                        .ifPresent(supplierInvalidEnum -> target.setStatus(supplierInvalidEnum.getValue()));
            }
        }

        /**
         * 转换
         *
         * @param source
         * @return
         */
        List<Carrier> convertSupplier(List<SupplierDO> source);
    }

    public static List<Carrier> convertOffline(List<SymbolDO> creation) {
        return CONVERT.convertSymbol(creation);
    }

    public static Carrier convertOffline(SymbolDO creation) {
        return CONVERT.convertSymbol(creation);
    }

    public static List<Carrier> convertOnline(List<SupplierDO> creation) {
        return CONVERT.convertSupplier(creation);
    }

    public static Carrier convertOnline(SupplierDO creation) {
        return CONVERT.convertSupplier(creation);
    }

}
